<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>路径总和 - 算法详解</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #282c34;
            color: #abb2bf;
            border-radius: 12px;
            overflow: hidden;
        }
        .code-block pre {
            padding: 1.5rem;
            overflow-x: auto;
        }
        .keyword { color: #c678dd; }
        .function { color: #61afef; }
        .string { color: #98c379; }
        .number { color: #d19a66; }
        .comment { color: #5c6370; font-style: italic; }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 1;
            font-weight: 700;
            margin-right: 0.5rem;
            color: #667eea;
            font-family: 'Noto Serif SC', serif;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        .complexity-badge {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 0.5rem 1rem;
            border-radius: 9999px;
            font-weight: 500;
            display: inline-block;
            margin: 0.25rem;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl md:text-6xl font-bold mb-4">
                <i class="fas fa-tree mr-4"></i>路径总和
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto">
                探索二叉树中的路径奥秘，用递归思维解决经典算法问题
            </p>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12 max-w-6xl">
        <!-- Problem Description Card -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <div class="flex items-center mb-6">
                <div class="bg-purple-100 p-3 rounded-full mr-4">
                    <i class="fas fa-question-circle text-purple-600 text-2xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">题目描述</h2>
            </div>
            <p class="text-lg text-gray-700 leading-relaxed">
                <span class="drop-cap">给</span>定一个二叉树和一个目标和，判断该树中是否存在根节点到叶子节点的路径，这条路径上所有节点值相加等于目标和。例如，给定二叉树 [5,4,8,11,null,13,4,7,2,null,null,null,1] 和目标和 22，存在路径 5→4→11→2，节点值之和为 22。
            </p>
        </div>

        <!-- Visual Example -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <div class="flex items-center mb-6">
                <div class="bg-blue-100 p-3 rounded-full mr-4">
                    <i class="fas fa-diagram-project text-blue-600 text-2xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">可视化示例</h2>
            </div>
            <div class="mermaid">
                graph TD
                    A[5] --> B[4]
                    A --> C[8]
                    B --> D[11]
                    B --> E[null]
                    C --> F[13]
                    C --> G[4]
                    D --> H[7]
                    D --> I[2]
                    G --> J[null]
                    G --> K[1]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style B fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style D fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style I fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
            </div>
            <p class="text-center text-gray-600 mt-4">
                <i class="fas fa-info-circle mr-2"></i>紫色节点表示目标路径：5 → 4 → 11 → 2 = 22
            </p>
        </div>

        <!-- Core Concepts -->
        <div class="grid md:grid-cols-2 gap-8 mb-8">
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="flex items-center mb-6">
                    <div class="bg-green-100 p-3 rounded-full mr-4">
                        <i class="fas fa-brain text-green-600 text-2xl"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800">核心考点</h3>
                </div>
                <div class="space-y-3">
                    <div class="flex items-center">
                        <i class="fas fa-check-circle text-green-500 mr-3"></i>
                        <span class="text-lg">递归思维</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-check-circle text-green-500 mr-3"></i>
                        <span class="text-lg">深度优先搜索（DFS）</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-check-circle text-green-500 mr-3"></i>
                        <span class="text-lg">二叉树遍历</span>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="flex items-center mb-6">
                    <div class="bg-orange-100 p-3 rounded-full mr-4">
                        <i class="fas fa-clock text-orange-600 text-2xl"></i>
                    </div>
                    <h3 class="text-2xl font-bold text-gray-800">复杂度分析</h3>
                </div>
                <div class="space-y-4">
                    <div>
                        <span class="complexity-badge">
                            <i class="fas fa-hourglass-half mr-2"></i>时间复杂度：O(n)
                        </span>
                        <p class="text-gray-600 mt-2">需要遍历所有节点</p>
                    </div>
                    <div>
                        <span class="complexity-badge">
                            <i class="fas fa-memory mr-2"></i>空间复杂度：O(h)
                        </span>
                        <p class="text-gray-600 mt-2">递归调用栈的深度</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- Solution Approach -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <div class="flex items-center mb-6">
                <div class="bg-indigo-100 p-3 rounded-full mr-4">
                    <i class="fas fa-lightbulb text-indigo-600 text-2xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">解题思路</h2>
            </div>
            <div class="grid md:grid-cols-3 gap-6">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                    <div class="text-4xl font-bold text-purple-600 mb-3">1</div>
                    <h4 class="font-bold text-lg mb-2">基础判断</h4>
                    <p class="text-gray-700">如果当前节点为空，直接返回 False</p>
                </div>
                <div class="bg-gradient-to-br from-blue-50 to-cyan-50 p-6 rounded-xl">
                    <div class="text-4xl font-bold text-blue-600 mb-3">2</div>
                    <h4 class="font-bold text-lg mb-2">叶子节点检查</h4>
                    <p class="text-gray-700">到达叶子节点时，判断路径和是否等于目标值</p>
                </div>
                <div class="bg-gradient-to-br from-green-50 to-emerald-50 p-6 rounded-xl">
                    <div class="text-4xl font-bold text-green-600 mb-3">3</div>
                    <h4 class="font-bold text-lg mb-2">递归探索</h4>
                    <p class="text-gray-700">递归检查左右子树，目标值减去当前节点值</p>
                </div>
            </div>
        </div>

        <!-- Code Implementation -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
            <div class="flex items-center mb-6">
                <div class="bg-red-100 p-3 rounded-full mr-4">
                    <i class="fas fa-code text-red-600 text-2xl"></i>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">示例代码</h2>
            </div>
            <div class="code-block">
                <pre><code><span class="keyword">def</span> <span class="function">hasPathSum</span>(root, targetSum):
    <span class="comment"># 基础情况：空节点</span>
    <span class="keyword">if not</span> root:
        <span class="keyword">return</span> <span class="keyword">False</span>
    
    <span class="comment"># 叶子节点：检查路径和</span>
    <span class="keyword">if not</span> root.left <span class="keyword">and not</span> root.right:
        <span class="keyword">return</span> targetSum == root.val
    
    <span class="comment"># 递归检查左右子树</span>
    <span class="keyword">return</span> (<span class="function">hasPathSum</span>(root.left, targetSum - root.val) <span class="keyword">or</span> 
            <span class="function">hasPathSum</span>(root.right, targetSum - root.val))</code></pre>
            </div>
        </div>

        <!-- Key Insights -->
        <div class="bg-gradient-to-r from-purple-600 to-pink-600 text-white rounded-2xl shadow-xl p-8 card-hover">
            <div class="flex items-center mb-6">
                <i class="fas fa-star text-3xl mr-4"></i>
                <h2 class="text-3xl font-bold">关键洞察</h2>
            </div>
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white bg-opacity-20 backdrop-blur-sm rounded-xl p-6">
                    <i class="fas fa-route text-2xl mb-3"></i>
                    <h4 class="font-bold text-xl mb-2">路径定义</h4>
                    <p>路径必须从根节点到叶子节点，不能在中间节点停止</p>
                </div>
                <div class="bg-white bg-opacity-20 backdrop-blur-sm rounded-xl p-6">
                    <i class="fas fa-minus-circle text-2xl mb-3"></i>
                    <h4 class="font-bold text-xl mb-2">目标值递减</h4>
                    <p>每经过一个节点，从目标值中减去该节点的值，简化问题</p>
                </div>
            </div>
        </div>
    </div>

    <script>
        mermaid.initialize({ 
            startOnLoad: true,
            theme: 'default',
            themeVariables: {
                primaryColor: '#667eea',
                primaryTextColor: '#fff',
                primaryBorderColor: '#7c3aed',
                lineColor: '#5a67d8',
                secondaryColor: '#006100',
                tertiaryColor: '#fff'
            }
        });
    </script>
</body>
</html>